import moment from 'moment';
import { get } from 'lodash';
import { Fixed } from 'gantd/lib/grid';

const renderTime = (time: string, formart: string = 'YYYY-MM-DD') => {
  let result = moment(time).format(formart);
  if (result == 'Invalid date') result = '-';
  return time && result;
};

// 表格列设置
const SettlementSchema = [
  {
    fieldName: 'partAssemblyExt.partPss',
    title: 'PSS',
    width: 50,
    fixed: Fixed.left,
    cellClass: 'stringType',
  },
  {
    fieldName: 'partAssemblyExt.partPssZhDesc',
    title: 'PSS名称',
    width: 60,
    fixed: Fixed.left,
  },
  {
    fieldName: 'partAssemblyExt.partFg',
    title: 'FG',
    width: 50,
    fixed: Fixed.left,
    cellClass: 'stringType',
  },
  {
    fieldName: 'partAssemblyExt.partFgZhDesc',
    title: 'FG名称',
    width: 60,
    fixed: Fixed.left,
  },
  {
    fieldName: 'partAssemblyExt.partPos',
    title: 'POS',
    fixed: Fixed.left,
    width: 50,
  },
  {
    fieldName: 'partAssemblyExt.partPosZhDesc',
    title: 'POS名称',
    width: 60,
    fixed: Fixed.left,
  },
  {
    fieldName: 'operationType',
    title: '操作类型',
    width: 65,
    valueFormatter: (params: any) => {
      const manual = get(params.data, 'partAssemblyExt.manual');
      if (manual) {
        // 来源手动新增的一直展示为手动新增
        return 'XBOM_ADD_MANUAL';
      }
      return params.value;
    },
    // 列过滤项的设置
    render: (text, data) => {
      const operationType = get(data, 'operationType');
      if (operationType !== 'XBOM_ADD_SOLVE') {
        return <span style={{ color: 'green' }}>{text}</span>;
      }
      // 初始化解算的数据不做特殊标识
      return <span>{text}</span>;
    },
  },
  {
    fieldName: 'levelPath',
    title: '层级',
    width: 50,
  },
  {
    fieldName: 'subPart.materialNum',
    title: '零件号',
    width: 220,
    showRowGroup: true,
    cellRenderer: 'gantGroupCellRenderer',
    headerClass: 'bom-grid-header',
    headerComponentParams: { onChange: undefined },
  },
  {
    fieldName: 'subPart.materialName',
    title: '零件名称',
    width: 100,
  },
  {
    fieldName: 'quantity',
    title: '用量',
    width: 50,
  },

  {
    fieldName: 'partAssemblyExt.supplyStatus',
    title: '供货状态',
    width: 65,
    valueGetter: (params: any) => get(params.data, 'partAssemblyExt.supplyStatus'),
    // 列过滤项的设置
    filterValueGetter: (params: any) => get(params.data, 'partAssemblyExt.supplyStatus'),
  },
  {
    fieldName: 'partAssemblyExt.findNo',
    title: 'findNo',
    width: 65,
  },
  {
    fieldName: 'subPart.materialExt.isColorPart',
    title: '是否颜色件',
    width: 70,
  },
  {
    fieldName: 'partAssemblyExt.oldPart.materialNum',
    title: '原零件号',
    width: 100,
    render: (text, data) => {
      const manual = get(data, 'partAssemblyExt.manual');
      if (manual) {
        // 来源手动新增的不展示原零件号
        return null;
      }
      return <span>{text}</span>;
    },
  },
  {
    fieldName: 'partAssemblyExt.oldQuantity',
    title: '原用量',
    width: 50,
    render: (text, data) => {
      const manual = get(data, 'partAssemblyExt.manual');
      if (manual) {
        // 来源手动新增的不展示原零件号
        return null;
      }
      return <span>{text}</span>;
    },
  },
  {
    fieldName: 'partAssemblyExt.plantCode',
    title: '工厂代码',
    width: 65,
    cellClass: 'stringType',
  },
  {
    fieldName: 'partAssemblyExt.workShopId',
    title: '车间代码',
    width: 65,
    cellClass: 'stringType',
    valueGetter: (params: any) => {
      return get(params.data, 'partAssemblyExt.workshopCode'); // 展示code，传值workShopId
    },
  },
  {
    fieldName: 'partAssemblyExt.workshopName',
    title: '车间描述',
    width: 60,
  },
  {
    fieldName: 'partAssemblyExt.processRouteCode',
    title: '工艺路线',
    width: 60,
    valueGetter: (params: any) => {
      const { data } = params;
      return data?.partAssemblyExt?.processRouteCode || '';
    },
  },
  {
    fieldName: 'partAssemblyExt.manual',
    title: '数据来源',
    width: 65,
    valueFormatter: (params: any) => {
      const manual = get(params.data, 'partAssemblyExt.manual');
      const levelPath = get(params.data, 'levelPath');
      return +levelPath !== 0 ? (manual ? '手动新增' : '解算') : '';
    },
    filterValueGetter: (params: any) => {
      const manual = get(params.data, 'partAssemblyExt.manual');
      const levelPath = get(params.data, 'levelPath');
      return +levelPath !== 0 ? (manual ? '手动新增' : '解算') : '';
    },
  },
  {
    fieldName: 'lineNum',
    title: 'BOM行标识',
    width: 150,
  },
  {
    fieldName: 'partAssemblyExt.bomCategory',
    title: 'BOM类型',
    width: 70,
  },
  {
    fieldName: 'partAssemblyExt.trackModel',
    title: '多轨件标识',
    width: 70,
  },
  {
    fieldName: 'subPart.materialExt.swSubType',
    title: '软件sub类型',
    width: 90,
  },
  {
    fieldName: 'partAssemblyExt.replaceableGroup',
    title: '工程替换组',
    width: 100,
  },
  {
    fieldName: 'partAssemblyExt.mainPart',
    title: '主要件',
    width: 100,
  },
  {
    fieldName: 'updateDate',
    title: '最后一次操作时间',
    width: 135,
    valueGetter: (params: any) => renderTime(get(params.data, get(params, 'colDef.field'), 'YYYY-MM-DD HH:mm:ss')),
  },
  {
    fieldName: 'updatedBy',
    title: '最后一次操作人员',
    width: 120,
  },
  {
    fieldName: 'usageValue',
    title: '变量表达式',
    width: 100,
  },
  {
    fieldName: 'subPart.materialNameEn',
    title: '零件英文名称',
    width: 200,
  },
];

export default SettlementSchema;
